#include <gtk/gtk.h>
#include <stdio.h>

#include "gtkutils.h"
#include "cjsonutils.h"
#include "stringutils.h"

void sayHello() {
    printf("hello word\n");
}
GtkTextView *textview;
GtkTextBuffer *buffer;
createSignalObjectFunc(onclick, GtkEntry) {
    const char *text = entryGetText(user_data);
    cJson json = NULL;
    if ((json = toJsonObject(text)) != NULL) {
        char *format = toStringJson(json);
        gtk_text_buffer_set_text(buffer, format, strlen(format));
        free(format);
        deleteJsonObject(json);
    }
}

createSignalObjectFunc(onclick_unformat, GtkEntry) {
    const char *text = entryGetText(user_data);
    cJson json = NULL;
    if ((json = toJsonObject(text)) != NULL) {
        char *format = toStringUnformatJson(json);
        gtk_text_buffer_set_text(buffer, format, strlen(format));
        free(format);
        deleteJsonObject(json);
    }
}

createSignalObjectFunc(onclick_unformat_translation, GtkEntry) {
    const char *text = entryGetText(user_data);
    cJson json = NULL;
    if ((json = toJsonObject(text)) != NULL) {
        char *format = toStringUnformatJson(json);
        char *format_translation = LineReplace(format, "\"","\\\"");
        gtk_text_buffer_set_text(buffer, format_translation, strlen(format_translation));
        free(format_translation);
        free(format);
        deleteJsonObject(json);
    }
}

createGAsyncReadyCallbackSourceObjectFunc(paste_receiver, GdkClipboard, GtkEntry) {
    GError *error = NULL;
    
    gchar *text;
    if((text = clipboardReadTextFinish(source_object, result, &error))) {
        entrySetText(user_data, text);
        free(text);
    }
}

createSignalObjectFunc(onpaste, GtkEntry) {
    GdkClipboard *clipboard = createClipboardWithWidget(user_data);
    clipboardReadTextAsync(clipboard, NULL, paste_receiver, user_data);
}

createSignalFunc(oncopy) {
    GdkClipboard *clipboard = createClipboardWithWidget(user_data);
    // const char *text = entryGetText(user_data);
    gtk_text_buffer_copy_clipboard(buffer, clipboard);

    // cJson json = NULL;
    // if ((json = toJsonObject(text)) != NULL) {
    //     char *format = toStringJson(json);
    //     setClipboardText(clipboard, format);
    //     free(format);
    //     deleteJsonObject(json);
    // }
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;

    window = createWindowWithTitleSize(app, "gtk-json-test", 600, 300);

    buffer = createTextBuffer(NULL);
    textview = createTextViewWithTextBuffer(buffer);

    GtkEntry *entry = createEntry();

    GtkBox *mainBox = createBoxWithVerticalAppends(0, 
        // 控件放置中间 - 对齐
        createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER,
            createLabel("输入JSON数据:"),
            entry,
            createButtonAction(entry, "粘贴", onpaste),
            createButtonAction(entry, "转换", onclick),
            NULL
        ),
        createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER,
            createButtonAction(entry, "转换(不格式化)", onclick_unformat),
            createButtonAction(entry, "转换(不格式化+转义)", onclick_unformat_translation),
            createButtonAction(entry, "复制结果", oncopy), 
            NULL
        ),
        // 此处需要一个可用的 TextView
        createWidgetVerticalExpand(textview, true),
        NULL
    );
    
    // GTK_ALIGN_FILL

    setWidgetFullMargin(mainBox, 10);
    setWindowChild(window, mainBox);

    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.json", 
                                G_APPLICATION_FLAGS_NONE, activate);
    // app = gtk_application_new("gtk.example.template", G_APPLICATION_FLAGS_NONE);
    // g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    // status = g_application_run(G_APPLICATION(app), argc, argv);
    // g_object_unref(app);
    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}

